import sys
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtChart import (QChartView, QChart, QHorizontalBarSeries, QBarSet,
                           QLegend, QBarCategoryAxis, QValueAxis)


class DemoHorizontalBarSeries(QMainWindow):
    def __init__(self, parent=None):
        super(DemoHorizontalBarSeries, self).__init__(parent)

        # 设置窗口标题
        self.setWindowTitle('实战 Qt for Python: 水平柱状图演示')
        # 设置窗口大小
        self.resize(480, 360)

        self.createChart()

    def createChart(self):
        # 创建条状单元
        barSet0 = QBarSet('Jane')
        barSet1 = QBarSet('Jone')
        barSet2 = QBarSet('Axel')
        barSet3 = QBarSet('Mary')
        barSet4 = QBarSet('Samantha')
        t = 60
        barSet0.append([t, 2, 3, 4, 5, 6])
        barSet1.append([5, 0, 0, 4, 0, 7])
        barSet2.append([3, 5, 8, 13, 8, 5])
        barSet3.append([5, 6, 7, 3, 4, 5])
        barSet4.append([9, 7, 5, 3, 1, 2])
        '''第二种写法
        barSet0 << 1 << 2 << 3 << 4 << 5 << 6
        barSet1 << 5 << 0 << 0 << 4 << 0 << 7
        barSet2 << 3 << 5 << 8 << 13 << 8 << 5
        barSet3 << 5 << 6 << 7 << 3 << 4 << 5
        barSet4 << 9 << 7 << 5 << 3 << 1 << 2
        '''



        # 条状图
        barSeries = QHorizontalBarSeries()
        barSeries.append(barSet0)
        barSeries.append(barSet1)
        barSeries.append(barSet2)
        barSeries.append(barSet3)
        barSeries.append(barSet4)
        barSeries.setLabelsVisible(1)#显示柱状图上的数字

        # 创建图表
        chart = QChart()
        chart.addSeries(barSeries)
        chart.setTitle('简单水平柱状图示例')
        chart.setAnimationOptions(QChart.SeriesAnimations)  # 设置成动画显示
        chart.setTheme(QChart.ChartThemeDark)#修改颜色

        # 设置横向坐标(X轴)
        categories = ['一月', '二月', '三月', '四月', '五月', '六月']
        axisY = QBarCategoryAxis()
        axisY.append(categories)
        chart.addAxis(axisY, Qt.AlignLeft)
        barSeries.attachAxis(axisY)

        # 设置纵向坐标(Y轴)
        axisX = QValueAxis()
        axisX.setRange(0, t +30)
        axisX.setTickCount(10)
        chart.addAxis(axisX, Qt.AlignBottom)
        barSeries.attachAxis(axisX)

        # 图例属性
        chart.legend().setVisible(True)
        chart.legend().setAlignment(Qt.AlignBottom)

        # 图表视图
        chartView = QChartView(chart)
        chartView.setRenderHint(QPainter.Antialiasing)

        self.setCentralWidget(chartView)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DemoHorizontalBarSeries()
    window.show()
    sys.exit(app.exec())